using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SwinMessengerLibrary;
using System.Runtime.InteropServices;

namespace SwinMessenger
{
    public partial class RosterPanel : UserControl
    {
        [DllImport("user32.dll")]
        static extern bool FlashWindow(IntPtr hwnd, bool bInvert);

        Roster _Roster;
        ImageList _ImageList;
        List<ChatForm> _ChatForms;

        public RosterPanel(Roster roster)
            : this()
        {
            _ChatForms = new List<ChatForm>();

            _Roster = roster;
            roster.OnRosterItem += new EventHandler(roster_OnRosterItem);
            roster.OnMessage += new EventHandler(roster_OnMessage);

            _ImageList = new ImageList();
            _ImageList.Images.AddRange(new Image[] {
                ImageBank.ItemAvailable, ImageBank.ItemAway,
                ImageBank.ItemBusy, ImageBank.ItemOffline
            });

            RosterList.SmallImageList = _ImageList;
            RosterList.LargeImageList = _ImageList;
            RosterList.DoubleClick += new EventHandler(RosterList_DoubleClick);
        }

        void roster_OnMessage(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new EventHandler(RosterList_DoubleClick),
                    new object[] { this, e });
                return;
            }
        }

        protected RosterPanel()
        {
            InitializeComponent();
        }

        void roster_OnRosterItem(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new EventHandler(roster_OnRosterItem),
                    new object[] { this, e });
                return;
            }
            if (e is ContactEventArgs)
            {
                ContactEventArgs cea = (ContactEventArgs)e;
                Contact contact = cea.Contact;
                ListViewItem item = FindItem(contact);
                if (item == null)
                {
                    item = new ListViewItem(contact.Id);
                    RosterList.Items.Add(item);
                }
                item.ImageIndex = GetItemImageIndex(contact.Status);
            }
        }

        void RosterList_DoubleClick(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new EventHandler(RosterList_DoubleClick),
                    new object[] { this, e });
                return;
            }
            string id = "";
            string message = "";
            bool found = true;
            try
            {
                id = RosterList.SelectedItems[0].Text;
            }
            catch { }
            if (e is MessageEventArgs)
            {
                MessageEventArgs mea = (MessageEventArgs)e;
                id = mea.Id;
                message = mea.Message;
            }
            if (!id.Equals("") && message != null)
            {
                Contact contact = _Roster.FindContact(id);
                ChatForm chatform = Find(contact.Id);
                if (chatform == null)
                {
                    chatform = new ChatForm(contact);
                    _ChatForms.Add(chatform);
                    chatform.Disposed += new EventHandler(chatform_Disposed);
                    found = false;
                }

                chatform.Show();
                chatform.BringToFront();
                FlashWindow(chatform.Handle, false);

                if (!found && !message.Equals(""))
                {
                    chatform.Contact.Chat(message, "");
                }
            }
        }

        void chatform_Disposed(object sender, EventArgs e)
        {
            if (sender is ChatForm)
            {
                ChatForm chatform = (ChatForm)sender;
                Remove(chatform.Contact.Id);
            }
        }

        public ListViewItem FindItem(Contact contact)
        {
            string id = contact.Id;
            foreach (ListViewItem item in RosterList.Items)
            {
                if (item.Text == id)
                {
                    return item;
                }
            }
            return null;
        }

        private int GetItemImageIndex(ContactStatus status)
        {
            switch (status)
            {
                case ContactStatus.Available:
                    return 0;
                case ContactStatus.Away:
                case ContactStatus.Idle:
                    return 1;
                case ContactStatus.Busy:
                    return 2;
                default:
                    return 3;
            }
        }

        public ChatForm Find(string id)
        {
            foreach (ChatForm form in _ChatForms)
            {
                if (form.Contact.Id.ToLower().Equals(id))
                {
                    return form;
                }
            }
            return null;
        }

        public void Remove(string id) {
            ChatForm chatform = Find(id);
            if(chatform != null) {
                _ChatForms.Remove(chatform);
            }
        }

        private void LogoutButton_Click(object sender, EventArgs e)
        {
            if (_Roster.Parent.Logout(false))
            {
                this.Dispose();
            }
        }
    }
}
